// pages/page7/page7.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    current: 1,
    chargeMoney: 5000
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({//获取滑块的高宽
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight,
          userInfo: wx.getStorageSync("userInfo"),
          userTypeName: wx.getStorageSync("userTypeName"),
          userBalance: wx.getStorageSync("userBalance"),
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
  , switchMoney: function (e) {
    this.setData({
      current: e.target.id,
      chargeMoney: e.target.dataset.money
    })
  }
  , charges: function (e) {
    charge(this);
  }
})

function charge(that, _money) {
  var s10 = Number(new Date());
  var s10Fromt = s10 ;
  // console.log("第一次得到值"+ Number(wx.getStorageSync('s10Fromt')));
  if (s10 - Number(wx.getStorageSync('s10Fromt')) > 2000) {
    wx.setStorageSync('s10Fromt', s10)
    //正常执行代码
    console.log("第一次得到值" + Number(wx.getStorageSync('s10Fromt')));
  } else {
    console.log("jjjj")
    
    wx.showModal({
      title: '提示',
      content: '提交太频繁了',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
    return false;
    //提交太频繁了。
  }
  if (wx.getStorageSync('userType') == 0) {
    wx.showModal({
      title: '提示',
      content: '您当前还不是庄主，请前往首页-成为庄主！',
      success: function (res) {
        if (res.confirm) {
          wx.switchTab({
            url: '../../pages/index/index',
          })
        } else if (res.cancel) {
        }
      }
    })
    return;
  }





  // console.log(wx.getStorageSync("userid"))
  wx.request({
    url: app.globalData.allUrl + "/tLPayAction/PayAgent",
    data: {
      userId: wx.getStorageSync("userid"),
      money: that.data.chargeMoney
      //  money: 0.01
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
      console.log(res);
      if (res.data != 250 && res.data != false) {
        var ss = JSON.parse(res.data.payinfo);
        console.log(ss)
        wx.requestPayment({//如果从后台获取支付信息成功。则调用微信支付接口
          timeStamp: ss.timeStamp,
          nonceStr: ss.nonceStr,
          package: ss.package,
          signType: "MD5",
          paySign: ss.paySign,
          success: function (res) {
            console.log(res)
            wx.showToast({
              title: '微信支付成功',
              icon: 'success',
              duration: 2000
            })

            wx.request({
              url: app.globalData.allUrl + "/userAction/getUserInfoById",
              data: {
                UUID: wx.getStorageSync("userid")
              },
              success: function (res) {
                console.log(res.data)
                var userTypeName = '';
                wx.setStorageSync("userType", res.data.isMerchant)
                wx.setStorageSync("isUpdatePrice", 1);
                wx.setStorageSync("userBalance", res.data.balance);
                wx.setStorageSync("cartLists", "");
                wx.setStorageSync("agentId", res.data.agentId);
                if (res.data.isMerchant == 1) {
                  userTypeName = "体验庄主"
                } else if (res.data.isMerchant == 2) {
                  userTypeName = "禾怡庄主"
                } else if (res.data.isMerchant == 3) {
                  userTypeName = "古堡庄主"
                } else if (res.data.isMerchant == 4) {
                  userTypeName = "金钥匙庄主"
                } else if (res.data.isMerchant == 5) {
                  userTypeName = "管理员庄主"
                }
                wx.setStorageSync('userTypeName', userTypeName)
                that.setData({
                  userBalance: res.data.balance,
                  userTypeName: userTypeName
                })
              }, fail: function (err) {
                console.log(err)
              }
            })
          }, fail(res) {
            wx.showModal({
              title: '提示',
              content: '支付失败！',
            })
          }
        })
      } else if (res.data == 250) {
        wx.showModal({
          title: '提示',
          content: '支付成功！商家正在审核中，请勿重复提交！',
        })
      } else if (res.data == false) {
        wx.showModal({
          title: '提示',
          content: '充值失败，请联系客服！',
        })
      }
    }
  })
}

